package com.lanyou.esb.cook.proxy.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.lanyou.esb.cook.proxy.entity.IfAvgResTimes;

/**
 * 接口响应时间持久层
 * 
 * @author Davey.wu
 */
public class IfAvgResTimesDao extends BaseDao<IfAvgResTimes> {
	private static final List<String> fields = new ArrayList<String>();
	/** SQL **/
	private static final String SQL;
	static {
		fields.add("IF_AVG_RES_TIMES_ID");
		fields.add("IF_ID");
		fields.add("AVG_RES_TIMES");
		fields.add("START_DATE");
		fields.add("INTERVAL_VAL");
		fields.add("INTERVAL_UNIT");
		fields.add("IS_ENABLE");
		fields.add("CREATOR");
		fields.add("CREATED_DATE");
		fields.add("MODIFIER");
		fields.add("LAST_UPDATED_DATE");
	}
	static {
		StringBuilder builderFields = new StringBuilder();
		StringBuilder builderValues = new StringBuilder();
		builderFields.append(" (");
		builderValues.append(" (");
		int size = fields.size();
		for (int i = 0; i < size; ++i) {
			builderFields.append(fields.get(i) + (i == size - 1 ? "" : ","));
			builderValues.append((i == size - 1 ? "?" : "?,"));
		}
		builderFields.append(") ");
		builderValues.append(") ");
		SQL = "insert into t_esb_bu_if_avg_res_times"
				+ builderFields.toString() + " values "
				+ builderValues.toString();
	}

	@Override
	public int save(IfAvgResTimes avgResTimes) {
		return getJdbcTemplate().update(SQL, avgResTimes.getId(),
				avgResTimes.getIfId(), avgResTimes.getAvgResTimes(),
				avgResTimes.getStartDate(), avgResTimes.getInterval(),
				avgResTimes.getIntervalUnit(), avgResTimes.getIsEnable(),
				avgResTimes.getCreator(), avgResTimes.getCreateDate(),
				avgResTimes.getModifier(), avgResTimes.getLastUpDate());
	}

	@Override
	public int saveAll(List<IfAvgResTimes> list) {
		if (list == null || list.isEmpty()) {
			return 0;
		}
		List<Object[]> args = new ArrayList<>();
		int i = 0;
		for (IfAvgResTimes avgResTimes : list) {
			i = 0;
			Object[] objects = new Object[fields.size()];
			objects[i++] = avgResTimes.getId();
			objects[i++] = avgResTimes.getIfId();
			objects[i++] = avgResTimes.getAvgResTimes();
			objects[i++] = avgResTimes.getStartDate();
			objects[i++] = avgResTimes.getInterval();
			objects[i++] = avgResTimes.getIntervalUnit();
			objects[i++] = avgResTimes.getIsEnable();
			objects[i++] = avgResTimes.getCreator();
			objects[i++] = avgResTimes.getCreateDate();
			objects[i++] = avgResTimes.getModifier();
			objects[i++] = avgResTimes.getLastUpDate();
			args.add(objects);
		}
		int[] result = getJdbcTemplate().batchUpdate(SQL, args);
		int num = 0;
		for (int k : result) {
			num += k;
		}
		return num;
	}
}
